﻿Public Class alarms

    Private Sub alarms_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        DataGridView1.DataSource = GetTable("SELECT * FROM vwAlarms")

        DataGridView1.Columns(1).HeaderText = "رقم الخطأ"
        DataGridView1.Columns(2).HeaderText = "مكان العطل"
        DataGridView1.Columns(3).HeaderText = "نوع العطل"
        DataGridView1.Columns(4).HeaderText = "تاريخ الحدوث"
        DataGridView1.Columns(5).HeaderText = "اسم الباتش"



        Dim dt As DataTable
        dt = GetTable("SELECT DISTINCT Alarm_unit FROM vwAlarms")
        cmbAlarmUnits.DataSource = dt
        cmbAlarmUnits.DisplayMember = "Alarm_unit"
        cmbAlarmUnits.DisplayMember = "Alarm_unit"

        dt = GetTable("SELECT * FROM Batchs")
        cmbBatches.DataSource = dt
        cmbBatches.DisplayMember = "BatchName"
        cmbBatches.ValueMember = "BatchName"

    End Sub

    Private Sub cbAlarmName_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbAlarmName.CheckedChanged
        If cbAlarmName.Checked = True Then
            cmbAlarmUnits.Enabled = True
        Else
            cmbAlarmUnits.Enabled = False
        End If
    End Sub

    Private Sub cbBatchName_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbBatchName.CheckedChanged
        If cbBatchName.Checked = True Then
            cmbBatches.Enabled = True
        Else
            cmbBatches.Enabled = False
        End If
    End Sub

    Private Sub cbDateSearch_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbDateSearch.CheckedChanged
        If cbDateSearch.Checked = True Then
            Date_From.Enabled = True
            Date_To.Enabled = True
            cbTimeSearch.Enabled = True
        Else
            Date_From.Enabled = False
            Date_To.Enabled = False
            cbTimeSearch.Enabled = False
            cbTimeSearch.Checked = False
        End If
    End Sub

    Private Sub cbTimeSearch_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbTimeSearch.CheckedChanged
        If cbTimeSearch.Checked = True Then
            tpFrom.Enabled = True
            tpTo.Enabled = True
        Else
            tpFrom.Enabled = False
            tpTo.Enabled = False
        End If
    End Sub

    Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click
        Dim dt As DataTable
        dt = Nothing
        Dim TimeFrom, TimeTo As String
        If cbTimeSearch.Checked = True Then
            TimeFrom = " " & tpFrom.Value.Hour & ":" & tpFrom.Value.Minute
            TimeTo = " " & tpTo.Value.Hour & ":" & tpTo.Value.Minute
        Else
            TimeFrom = " 00:00"
            TimeTo = " 23:59"
        End If

        If cbAlarmName.Checked = False And cbBatchName.Checked = False And cbDateSearch.Checked = False Then
            dt = GetTable("SELECT * From vwAlarms")
        ElseIf cbAlarmName.Checked = True And cbBatchName.Checked = False And cbDateSearch.Checked = False Then
            dt = GetTable("SELECT * From vwAlarms WHERE Alarm_Unit='" & cmbAlarmUnits.Text & "'")
        ElseIf cbAlarmName.Checked = False And cbBatchName.Checked = True And cbDateSearch.Checked = False Then
            dt = GetTable("SELECT * From vwAlarms WHERE BatchName='" & cmbBatches.Text & "'")
        ElseIf cbAlarmName.Checked = False And cbBatchName.Checked = False And cbDateSearch.Checked = True Then
            dt = GetTable("SELECT * From vwAlarms WHERE Alarm_Date >= '" & Date_From.Value.Year & "-" & Date_From.Value.Month & "-" & Date_From.Value.Day & TimeFrom & "' and Alarm_Date <= '" & Date_To.Value.Year & "-" & Date_To.Value.Month & "-" & Date_To.Value.Day & TimeTo & "'")
        ElseIf cbAlarmName.Checked = True And cbBatchName.Checked = True And cbDateSearch.Checked = False Then
            dt = GetTable("SELECT * From vwAlarms WHERE BatchName='" & cmbBatches.Text & "' AND Alarm_Unit='" & cmbAlarmUnits.Text & "'")
        ElseIf cbAlarmName.Checked = True And cbBatchName.Checked = False And cbDateSearch.Checked = True Then
            dt = GetTable("SELECT * From vwAlarms WHERE Alarm_Unit='" & cmbAlarmUnits.Text & "' AND Alarm_Date >= '" & Date_From.Value.Year & "-" & Date_From.Value.Month & "-" & Date_From.Value.Day & TimeFrom & "' and Alarm_Date <= '" & Date_To.Value.Year & "-" & Date_To.Value.Month & "-" & Date_To.Value.Day & TimeTo & "'")
        ElseIf cbAlarmName.Checked = True And cbBatchName.Checked = True And cbDateSearch.Checked = True Then
            dt = GetTable("SELECT * From vwAlarms WHERE Alarm_Unit='" & cmbAlarmUnits.Text & "' AND BatchName='" & cmbBatches.Text & "' AND Alarm_Date >= '" & Date_From.Value.Year & "-" & Date_From.Value.Month & "-" & Date_From.Value.Day & TimeFrom & "' and Alarm_Date <= '" & Date_To.Value.Year & "-" & Date_To.Value.Month & "-" & Date_To.Value.Day & TimeTo & "'")
        ElseIf cbAlarmName.Checked = False And cbBatchName.Checked = True And cbDateSearch.Checked = True Then
            dt = GetTable("SELECT * From vwAlarms WHERE BatchName='" & cmbBatches.Text & "' AND Alarm_Date >= '" & Date_From.Value.Year & "-" & Date_From.Value.Month & "-" & Date_From.Value.Day & TimeFrom & "' and Alarm_Date <= '" & Date_To.Value.Year & "-" & Date_To.Value.Month & "-" & Date_To.Value.Day & TimeTo & "'")
        End If
        DataGridView1.DataSource = dt
    End Sub

    Private Sub btnSelectAll_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSelectAll.Click
        Dim n As Integer
        n = DataGridView1.Rows.Count - 1

        If btnSelectAll.Text = "Select All" Then
            For i As Integer = 0 To n
                DataGridView1.Rows(i).Cells(0).Value = True
            Next
            btnSelectAll.Text = "Deselect All"
        Else
            For i As Integer = 0 To n
                DataGridView1.Rows(i).Cells(0).Value = False
            Next
            btnSelectAll.Text = "Select All"
        End If

    End Sub

    Private Sub btnDeleteSelected_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDeleteSelected.Click
        Dim n As Integer
        n = DataGridView1.Rows.Count - 1

        For i As Integer = 0 To n
            If DataGridView1.Rows(i).Cells(0).Value = True Then
                ExecuteSQL("DELETE FROM Alarms WHERE AlarmID='" & DataGridView1.Rows(i).Cells(1).Value & "'")
            End If
        Next
        DataGridView1.DataSource = GetTable("SELECT * From vwAlarms")
    End Sub
End Class